package keeto.com.vn.classified.utils;

import java.io.File;

import keeto.com.vn.classified.config.IConfig;

import android.content.Context;
import android.os.Environment;
import android.text.SpannableStringBuilder;

public class StringUtil {

	public static String URLencode(String s) {
		if (s != null) {
			StringBuffer tmp = new StringBuffer();
			int i = 0;
			try {
				while (true) {
					int b = s.charAt(i++);
					if ((b >= 0x30 && b <= 0x39) || (b >= 0x41 && b <= 0x5A)
							|| (b >= 0x61 && b <= 0x7A)) {
						tmp.append((char) b);
					} else {
						tmp.append("%");
						if (b <= 0xf) {
							tmp.append("0");
						}
						tmp.append(Integer.toHexString(b));
					}
				}
			} catch (Exception e) {
			}
			return tmp.toString();
		}
		return null;
	}

	/**
	 * 
	 * @return 
	 */
	public static String getTempFileDir() {

		File externalStore = Environment.getExternalStorageDirectory();

		if (externalStore != null) {
			String dir = externalStore.getAbsolutePath() + IConfig.PATH_TEMP;
			File file = new File(dir);
			if (!file.exists()) {
				file.mkdirs();
			}
			return dir;
		}

		return null;
	}

	public static String getImgTempFileDir() {

		File externalStore = Environment.getExternalStorageDirectory();

		if (externalStore != null) {
			String dir = externalStore.getAbsolutePath() + IConfig.PATH_TEMP;
			File file = new File(dir);
			if (!file.exists()) {
				file.mkdirs();
			}
			return dir;
		}

		return null;
	}
	
	/**
	 * @category 获取下载的文件路径
	 * */
	public static String getDownloadFileDir() {
		File externalStore = Environment.getExternalStorageDirectory();

		if (externalStore != null) {
			String dir = externalStore.getAbsolutePath() + IConfig.PATH_DOWNLOAD;
			File file = new File(dir);
			if (!file.exists()) {
				file.mkdirs();
			}
			return dir;
		}

		return null;
	}

	/**
	 * @category 获取上传的文件路径
	 * */
	public static String getUploadFileDir() {
		File externalStore = Environment.getExternalStorageDirectory();

		if (externalStore != null) {
			String dir = externalStore.getAbsolutePath() + IConfig.PATH_UPLOAD;
			File file = new File(dir);
			if (!file.exists()) {
				file.mkdirs();
			}
			return dir;
		}

		return null;
	}
	

	public static String getHost(String url){
	    if(url == null || url.length() == 0)
	        return "";

	    int doubleslash = url.indexOf("//");
	    if(doubleslash == -1)
	        doubleslash = 0;
	    else
	        doubleslash += 2;

	    int end = url.indexOf('/', doubleslash);
	    end = end >= 0 ? end : url.length();

	    return url.substring(doubleslash, end);
	}
}
